import java.util.HashMap;

public class Main {
    /**
     * 四数相加 II
     * https://leetcode.cn/problems/4sum-ii/
     * 给你四个整数数组 nums1、nums2、nums3 和 nums4 ，数组长度都是 n ，请你计算有多少个元组 (i, j, k,
     * l) 能满足：
     * 0 <= i, j, k, l < n
     * nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
     */
    public static void main(String[] args) {
        System.out.println("hello world 0_<");
        int[] nums1 = {1,2}, nums2 = {-2,-1}, nums3 = {-1,2}, nums4 = {0,2};
        System.out.println(new Main().fourSumCount(nums1, nums2, nums3, nums4));
    }
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer, Integer> sumABs = new HashMap<>();
        int result = 0;
        int sumAB, sumCD;
        for(int num1: nums1) {
            for (int num2: nums2) {
                sumAB = num1 + num2;
                sumABs.put(sumAB, sumABs.get(sumAB) == null ? 1 : sumABs.get(sumAB) + 1);
            }
        }
        for (int num3: nums3) {
            for (int num4: nums4) {
                sumCD = num3 + num4;
                if (sumABs.containsKey(-sumCD)) {
                    result += sumABs.get(-sumCD);
                }
            }
        }
        return result;
    }
}
